package server;

import java.io.*;
import java.net.Socket;

public class MessageServer {

    private static MessageServer instance = new MessageServer();

    public static MessageServer getInstance(){
        return instance;
    }

    private Socket socket;

    private OutputStream os;

    private InputStream is;

    private ObjectInputStream ois;

    private ObjectOutputStream oos;

    private String serverIP = "localhost";

    private int serverPort = 8888;

    private boolean isConnected = false;

    private MessageServer() {
    }

    public boolean connectToServer(){
        try {
            socket =  new Socket(serverIP,serverPort);
            socket.setSoTimeout(20000);
            os = socket.getOutputStream();
            is = socket.getInputStream();
            oos = new ObjectOutputStream(os);
            ois = new ObjectInputStream(is);
            System.out.println("客户端连接服务端成功："+socket.getInetAddress()+",port="+serverPort);
            isConnected = true;
            return isConnected;
        } catch (IOException e) {
            System.out.println("客户端连接服务端失败");
            return isConnected;
        }
    }

    public Socket getSocket() {
        return socket;
    }

    public OutputStream getOs() {
        return os;
    }

    public InputStream getIs() {
        return is;
    }

    public ObjectInputStream getOis() {
        return ois;
    }

    public ObjectOutputStream getOos() {
        return oos;
    }

    public boolean isConnected() {
        return isConnected;
    }
}
